package com.shilan.administrator.qintfm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.shilan.administrator.qintfm.R;
import com.shilan.administrator.qintfm.beans.RecommendResult;
import com.shilan.administrator.qintfm.tools.MyOnclickListener;
import com.shilan.administrator.qintfm.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/21.
 */
public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.RecommendViewHolder> {

    private Context context;
    private ImageLoader imageLoader;
    private RecommendResult.HotRecommendsEntity hot;
    //private LayoutInflater inflater;
    private MyOnclickListener mylistener;


    public RecommendAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance()._getImageLoader();
    }
    public void addHotData(RecommendResult.HotRecommendsEntity hot) {
        this.hot = hot;
        notifyDataSetChanged();
    }

    @Override
    public RecommendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view = LayoutInflater.from(context).inflate(R.layout.recommend_adapter_item,parent,false);
        return new RecommendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecommendViewHolder holder, int position) {

        if (holder instanceof RecommendViewHolder) {
            RecommendViewHolder rHolder = (RecommendViewHolder) holder;
            if (hot != null) {
                // 获得第一个list对象
                List<RecommendResult.HotRecommendsEntity.ListEntity> hotList = hot.getList();
                rHolder.TitleTv.setText(hotList.get(position).getTitle());

                if (hotList != null && hotList.size() > 0) {

                    List<RecommendResult.HotRecommendsEntity.ListEntity.ListEntityList> hotListEntity =
                            hotList.get(position).getListEntList();
                    if (hotListEntity != null && hotListEntity.size() > 0) {
                        rHolder.tvFirst_recommend_card.setText(hotListEntity.get(0).getTrackTitle());

                        rHolder.ivFirst_recommend_card.setImageUrl(hotListEntity.get(0).getCoverLarge(), imageLoader);
                        rHolder.tvSecond_recommend_card.setText(hotListEntity.get(1).getTrackTitle());

                        rHolder.ivSecond_recommend_card.setImageUrl(hotListEntity.get(1).getCoverLarge(),imageLoader );
                        rHolder.tvThird_recommend_card.setText(hotListEntity.get(2).getTrackTitle());

                        rHolder.ivThird_recommend_card.setImageUrl(hotListEntity.get(2).getCoverLarge(), imageLoader);

                    }
                }

            }

        }

    }

    @Override
    public int getItemCount() {
        return hot != null && hot.getList().size() > 0 ?
                hot.getList().size(): 0;
    }



    class RecommendViewHolder extends  RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView TitleTv, TitleMore,tvFirst_recommend_card,tvSecond_recommend_card,tvThird_recommend_card;
        private NetworkImageView ivFirst_recommend_card,ivSecond_recommend_card,ivThird_recommend_card;
        private LinearLayout linearFirstRecommend, linearSecondRecommend, linearThirdRecommend;

        /**
         * cardview 组键
         * **/
        public RecommendViewHolder(View itemView) {
            super(itemView);
            TitleTv = (TextView) itemView.findViewById(R.id.tv_title_card);
            TitleMore = (TextView) itemView.findViewById(R.id.tv_title_more);
            tvFirst_recommend_card = (TextView) itemView.findViewById(R.id.tvFirst_recommend_card);
            tvSecond_recommend_card = (TextView) itemView.findViewById(R.id.tvSecond_recommend_card);
            tvThird_recommend_card = (TextView) itemView.findViewById(R.id.tvThird_recommend_card);
            ivFirst_recommend_card = (NetworkImageView) itemView.findViewById(R.id.ivFirst_recommend_card);
            ivSecond_recommend_card = (NetworkImageView) itemView.findViewById(R.id.ivSecond_recommend_card);
            ivThird_recommend_card = (NetworkImageView) itemView.findViewById(R.id.ivThird_recommend_card);

            linearFirstRecommend = (LinearLayout) itemView.findViewById(R.id.linearFirstRecommend);
            linearSecondRecommend = (LinearLayout) itemView.findViewById(R.id.linearSecondRecommend);
            linearThirdRecommend = (LinearLayout) itemView.findViewById(R.id.linearThirdRecommend);

            linearFirstRecommend.setOnClickListener(this);
            linearSecondRecommend.setOnClickListener(this);
            linearThirdRecommend.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.linearFirstRecommend:
                    mylistener.myListener(hot.getList().get(0).getCategoryId(), 0);
                    break;
                case R.id.linearSecondRecommend:
                    mylistener.myListener(hot.getList().get(1).getCategoryId(), 0);
                    break;
                case R.id.linearThirdRecommend:
                    mylistener.myListener(hot.getList().get(2).getCategoryId(), 0);
                    break;
            }
        }
    }
}
